<?php
namespace app\api\controller;

class Money extends Permission
{
    public function getList()
    {
        $page = isset($this->input_data['page']) ? $this->input_data['page'] : 1;
        $limit = isset($this->input_data['limit']) ? $this->input_data['limit'] : 15;
        $map = [];

        if( isset($this->input_data['cate_id']) && $this->input_data['cate_id'] > 0 ) $map['cate_id'] = $this->input_data['cate_id'];
        if( isset($this->input_data['type']) && $this->input_data['type'] != '所有类型' ) $map['type'] = $this->input_data['type'];

        if(isset($this->input_data['action']))
        {
            $a = $this->input_data['action'];
            if($a == 'offer') $map['status'] = 0;
            if($a == 'choose'){
                $map['status'] = 0;
                $map['offer_times'] = ['>',0];
            }
            if($a == 'buy') $map['status'] = 10;
        }

        $total = db('Money')->where($map)->count();
        $rows = db('Money')->order('id', 'desc')->page($page,$limit)->where($map)->select();


        $list = [];
        foreach ($rows as $v) {
            
            $list[] = $v;
        }
        
        $data = [
            'total' => $total,
            'list' => $list
        ];
        $this->resSuc($data);
    }

    

    // 出单完成
    public function update()
    {
        $inputData = json_decode($this->input_data['data'],true);
        # 创建收入记录
        $data = [
            'price' => formatMoneySave($this->input_data['price']),
            'amount' => formatMoneySave($this->input_data['amount']),
            'related_id' => $inputData['id'],
            'create_time' => time(),
            'create_user' => $this->user['realname'],
            'create_user_id' => $this->user['id'],
        ];
        $moneyId = db('Money')->insertGetId($data);

        # 创建付款明细
        $this->_createMoneyDetail($moneyId);

        # 执行业务

        $this->resSuc('ok');
    }

    private function _createMoneyDetail()
    {
        // $price
        // if()
    }
}
